package com.example.myanimator.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/16
 * @describe
 */
public class SpiderViewPath extends View {

    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;
    private int centerX;
    private int centerY;
    private float radius;//网格最大半径

    public SpiderViewPath(Context context) {
        super(context);
    }

    public SpiderViewPath(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        mPath = new Path();
        mCanvas = new Canvas();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        radius = Math.min(w, h) * 0.9f / 2;
        centerX = w / 2;
        centerY = h / 2;
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {


        paintSpider(canvas);
//        paintSpiderBook(canvas);//这方法有问题
    }

    private void paintSpiderBook(Canvas canvas) {
//        mPaint.setColor(Color.GREEN);
        int count = 6;
        Path path = new Path();
        float r = radius/count;
        for (int i = 1; i <=6; i++) {
            float curR  = r*i;
            path.reset();
            for (int j=0;i<count;j++){
                if (j==0){
                    path.moveTo(centerX+curR,centerY);
                }else {
                    float x = (float)(centerX+curR*Math.cos(Math.toRadians(60)*j));
                    float y = (float)(centerY+curR*Math.sin(Math.toRadians(60)*j));
                    path.lineTo(x,y);
                }
            }

            path.close();
            canvas.drawPath(path,mPaint);
        }
    }
    /**画六边形*/
    private void paintSpider(Canvas canvas) {
        float y = (float) radius / 6  / (float) Math.sin(Math.toRadians(60));
        float r = radius / 6 /2;
        for (int i = 1; i <=6; i++) {
            mPath.moveTo(centerX - (i * r), centerY - (i * y));
            mPath.lineTo(centerX + (i * r), centerY  - (i * y));
            canvas.drawPath(mPath, mPaint);

//            mPath.moveTo(centerX + (i * r), centerY - (i * y));
            mPath.lineTo(centerX + (i * r*2), centerY );
            canvas.drawPath(mPath, mPaint);

//            mPath.moveTo(centerX + (i * r*2), centerY);
            mPath.lineTo(centerX + (i * r), centerY  + (i * y));
            canvas.drawPath(mPath, mPaint);

//            mPath.moveTo(centerX + (i * r), centerY  + (i * y));
            mPath.lineTo(centerX -  (i * r), centerY  + (i * y)  );
            canvas.drawPath(mPath, mPaint);

//            mPath.moveTo(centerX -  (i * r), centerY  + (i * y));
            mPath.lineTo(centerX -  (i * r*2), centerY  );
            canvas.drawPath(mPath, mPaint);

//            mPath.moveTo(centerX -  (i * r*2), centerY);
            mPath.lineTo(centerX - (i * r), centerY - (i * y) );
            canvas.drawPath(mPath, mPaint);
        }
    }
}
